package com.aiyige.page.authentication.model;

import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;

import com.aiyige.model.UserCertification;
import com.aiyige.utils.ListUtil;

import java.util.LinkedList;
import java.util.List;

public class Authentication implements Parcelable {
    public static final int USER_TYPE_PERSONAL = 1;
    public static final int USER_TYPE_ORGANIZATION = 2;
    public static final int USER_TYPE_UNKNOW = 3;
    public static final int STATUS_WAITING_AUTHENTICATION = 0;
    public static final int STATUS_AUTHENTICATION_SUCCESS = 1;
    public static final int STATUS_AUTHENTICATION_FAILED = 2;
    public static final int STATUS_NOT_AUTHENTICATION = 3;
    int userType;
    boolean readOnly;

    String companyName;
    String legalPersonName;
    String legalPersonIdNum;

    String businessLicenseLocalUrl;
    String businessLicenseRemoteUrl;

    String legalPersonIdCardFrontLocalUrl;
    String legalPersonIdCardFrontRemoteUrl;

    String legalPersonIdCardBackLocalUrl;
    String legalPersonIdCardBackRemoteUrl;


    String personalName;
    String personalIdNum;

    String personalHoldingIdCardLocalUrl;
    String personalHoldingIdCardRemoteUrl;

    String personalIdCardFrontLocalUrl;
    String personalIdCardFrontRemoteUrl;

    String personalIdCardBackLocalUrl;
    String personalIdCardBackRemoteUrl;

    long createTime;
    long updateTime;
    long auditTime;
    String auditor;
    String auditorIp;
    List<String> auditMsg;
    int auditStatus;

    public Authentication() {
    }

    private Authentication(Builder builder) {
        setUserType(builder.userType);
        setReadOnly(builder.readOnly);
        setCompanyName(builder.companyName);
        setLegalPersonName(builder.legalPersonName);
        setLegalPersonIdNum(builder.legalPersonIdNum);
        setBusinessLicenseLocalUrl(builder.businessLicenseLocalUrl);
        setBusinessLicenseRemoteUrl(builder.businessLicenseRemoteUrl);
        setLegalPersonIdCardFrontLocalUrl(builder.legalPersonIdCardFrontLocalUrl);
        setLegalPersonIdCardFrontRemoteUrl(builder.legalPersonIdCardFrontRemoteUrl);
        setLegalPersonIdCardBackLocalUrl(builder.legalPersonIdCardBackLocalUrl);
        setLegalPersonIdCardBackRemoteUrl(builder.legalPersonIdCardBackRemoteUrl);
        setPersonalName(builder.personalName);
        setPersonalIdNum(builder.personalIdNum);
        setPersonalHoldingIdCardLocalUrl(builder.personalHoldingIdCardLocalUrl);
        setPersonalHoldingIdCardRemoteUrl(builder.personalHoldingIdCardRemoteUrl);
        setPersonalIdCardFrontLocalUrl(builder.personalIdCardFrontLocalUrl);
        setPersonalIdCardFrontRemoteUrl(builder.personalIdCardFrontRemoteUrl);
        setPersonalIdCardBackLocalUrl(builder.personalIdCardBackLocalUrl);
        setPersonalIdCardBackRemoteUrl(builder.personalIdCardBackRemoteUrl);
        setCreateTime(builder.createTime);
        setUpdateTime(builder.updateTime);
        setAuditTime(builder.auditTime);
        setAuditor(builder.auditor);
        setAuditorIp(builder.auditorIp);
        setAuditMsg(builder.auditMsg);
        setAuditStatus(builder.auditStatus);
    }

    public static Builder newBuilder() {
        return new Builder();
    }

    public int getUserType() {
        return userType;
    }

    public void setUserType(int userType) {
        this.userType = userType;
    }

    public boolean isReadOnly() {
        return readOnly;
    }

    public void setReadOnly(boolean readOnly) {
        this.readOnly = readOnly;
    }

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    public String getLegalPersonName() {
        return legalPersonName;
    }

    public void setLegalPersonName(String legalPersonName) {
        this.legalPersonName = legalPersonName;
    }

    public String getLegalPersonIdNum() {
        return legalPersonIdNum;
    }

    public void setLegalPersonIdNum(String legalPersonIdNum) {
        this.legalPersonIdNum = legalPersonIdNum;
    }

    public String getBusinessLicenseLocalUrl() {
        return businessLicenseLocalUrl;
    }

    public void setBusinessLicenseLocalUrl(String businessLicenseLocalUrl) {
        this.businessLicenseLocalUrl = businessLicenseLocalUrl;
    }

    public String getBusinessLicenseRemoteUrl() {
        return businessLicenseRemoteUrl;
    }

    public void setBusinessLicenseRemoteUrl(String businessLicenseRemoteUrl) {
        this.businessLicenseRemoteUrl = businessLicenseRemoteUrl;
    }

    public String getLegalPersonIdCardFrontLocalUrl() {
        return legalPersonIdCardFrontLocalUrl;
    }

    public void setLegalPersonIdCardFrontLocalUrl(String legalPersonIdCardFrontLocalUrl) {
        this.legalPersonIdCardFrontLocalUrl = legalPersonIdCardFrontLocalUrl;
    }

    public String getLegalPersonIdCardFrontRemoteUrl() {
        return legalPersonIdCardFrontRemoteUrl;
    }

    public void setLegalPersonIdCardFrontRemoteUrl(String legalPersonIdCardFrontRemoteUrl) {
        this.legalPersonIdCardFrontRemoteUrl = legalPersonIdCardFrontRemoteUrl;
    }

    public String getLegalPersonIdCardBackLocalUrl() {
        return legalPersonIdCardBackLocalUrl;
    }

    public void setLegalPersonIdCardBackLocalUrl(String legalPersonIdCardBackLocalUrl) {
        this.legalPersonIdCardBackLocalUrl = legalPersonIdCardBackLocalUrl;
    }

    public String getLegalPersonIdCardBackRemoteUrl() {
        return legalPersonIdCardBackRemoteUrl;
    }

    public void setLegalPersonIdCardBackRemoteUrl(String legalPersonIdCardBackRemoteUrl) {
        this.legalPersonIdCardBackRemoteUrl = legalPersonIdCardBackRemoteUrl;
    }

    public String getPersonalName() {
        return personalName;
    }

    public void setPersonalName(String personalName) {
        this.personalName = personalName;
    }

    public String getPersonalIdNum() {
        return personalIdNum;
    }

    public void setPersonalIdNum(String personalIdNum) {
        this.personalIdNum = personalIdNum;
    }

    public String getPersonalHoldingIdCardLocalUrl() {
        return personalHoldingIdCardLocalUrl;
    }

    public void setPersonalHoldingIdCardLocalUrl(String personalHoldingIdCardLocalUrl) {
        this.personalHoldingIdCardLocalUrl = personalHoldingIdCardLocalUrl;
    }

    public String getPersonalHoldingIdCardRemoteUrl() {
        return personalHoldingIdCardRemoteUrl;
    }

    public void setPersonalHoldingIdCardRemoteUrl(String personalHoldingIdCardRemoteUrl) {
        this.personalHoldingIdCardRemoteUrl = personalHoldingIdCardRemoteUrl;
    }

    public String getPersonalIdCardFrontLocalUrl() {
        return personalIdCardFrontLocalUrl;
    }

    public void setPersonalIdCardFrontLocalUrl(String personalIdCardFrontLocalUrl) {
        this.personalIdCardFrontLocalUrl = personalIdCardFrontLocalUrl;
    }

    public String getPersonalIdCardFrontRemoteUrl() {
        return personalIdCardFrontRemoteUrl;
    }

    public void setPersonalIdCardFrontRemoteUrl(String personalIdCardFrontRemoteUrl) {
        this.personalIdCardFrontRemoteUrl = personalIdCardFrontRemoteUrl;
    }

    public String getPersonalIdCardBackLocalUrl() {
        return personalIdCardBackLocalUrl;
    }

    public void setPersonalIdCardBackLocalUrl(String personalIdCardBackLocalUrl) {
        this.personalIdCardBackLocalUrl = personalIdCardBackLocalUrl;
    }

    public String getPersonalIdCardBackRemoteUrl() {
        return personalIdCardBackRemoteUrl;
    }

    public void setPersonalIdCardBackRemoteUrl(String personalIdCardBackRemoteUrl) {
        this.personalIdCardBackRemoteUrl = personalIdCardBackRemoteUrl;
    }

    public long getCreateTime() {
        return createTime;
    }

    public void setCreateTime(long createTime) {
        this.createTime = createTime;
    }

    public long getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(long updateTime) {
        this.updateTime = updateTime;
    }

    public long getAuditTime() {
        return auditTime;
    }

    public void setAuditTime(long auditTime) {
        this.auditTime = auditTime;
    }

    public String getAuditor() {
        return auditor;
    }

    public void setAuditor(String auditor) {
        this.auditor = auditor;
    }

    public String getAuditorIp() {
        return auditorIp;
    }

    public void setAuditorIp(String auditorIp) {
        this.auditorIp = auditorIp;
    }

    public List<String> getAuditMsg() {
        return auditMsg;
    }

    public void setAuditMsg(List<String> auditMsg) {
        this.auditMsg = auditMsg;
    }

    public int getAuditStatus() {
        return auditStatus;
    }

    public void setAuditStatus(int auditStatus) {
        this.auditStatus = auditStatus;
    }

    public static final class Builder {
        private int userType;
        private boolean readOnly;
        private String companyName;
        private String legalPersonName;
        private String legalPersonIdNum;
        private String businessLicenseLocalUrl;
        private String businessLicenseRemoteUrl;
        private String legalPersonIdCardFrontLocalUrl;
        private String legalPersonIdCardFrontRemoteUrl;
        private String legalPersonIdCardBackLocalUrl;
        private String legalPersonIdCardBackRemoteUrl;
        private String personalName;
        private String personalIdNum;
        private String personalHoldingIdCardLocalUrl;
        private String personalHoldingIdCardRemoteUrl;
        private String personalIdCardFrontLocalUrl;
        private String personalIdCardFrontRemoteUrl;
        private String personalIdCardBackLocalUrl;
        private String personalIdCardBackRemoteUrl;
        private long createTime;
        private long updateTime;
        private long auditTime;
        private String auditor;
        private String auditorIp;
        private List<String> auditMsg;
        private int auditStatus;

        private Builder() {
        }

        public Builder userType(int val) {
            userType = val;
            return this;
        }

        public Builder readOnly(boolean val) {
            readOnly = val;
            return this;
        }

        public Builder companyName(String val) {
            companyName = val;
            return this;
        }

        public Builder legalPersonName(String val) {
            legalPersonName = val;
            return this;
        }

        public Builder legalPersonIdNum(String val) {
            legalPersonIdNum = val;
            return this;
        }

        public Builder businessLicenseLocalUrl(String val) {
            businessLicenseLocalUrl = val;
            return this;
        }

        public Builder businessLicenseRemoteUrl(String val) {
            businessLicenseRemoteUrl = val;
            return this;
        }

        public Builder legalPersonIdCardFrontLocalUrl(String val) {
            legalPersonIdCardFrontLocalUrl = val;
            return this;
        }

        public Builder legalPersonIdCardFrontRemoteUrl(String val) {
            legalPersonIdCardFrontRemoteUrl = val;
            return this;
        }

        public Builder legalPersonIdCardBackLocalUrl(String val) {
            legalPersonIdCardBackLocalUrl = val;
            return this;
        }

        public Builder legalPersonIdCardBackRemoteUrl(String val) {
            legalPersonIdCardBackRemoteUrl = val;
            return this;
        }

        public Builder personalName(String val) {
            personalName = val;
            return this;
        }

        public Builder personalIdNum(String val) {
            personalIdNum = val;
            return this;
        }

        public Builder personalHoldingIdCardLocalUrl(String val) {
            personalHoldingIdCardLocalUrl = val;
            return this;
        }

        public Builder personalHoldingIdCardRemoteUrl(String val) {
            personalHoldingIdCardRemoteUrl = val;
            return this;
        }

        public Builder personalIdCardFrontLocalUrl(String val) {
            personalIdCardFrontLocalUrl = val;
            return this;
        }

        public Builder personalIdCardFrontRemoteUrl(String val) {
            personalIdCardFrontRemoteUrl = val;
            return this;
        }

        public Builder personalIdCardBackLocalUrl(String val) {
            personalIdCardBackLocalUrl = val;
            return this;
        }

        public Builder personalIdCardBackRemoteUrl(String val) {
            personalIdCardBackRemoteUrl = val;
            return this;
        }

        public Builder createTime(long val) {
            createTime = val;
            return this;
        }

        public Builder updateTime(long val) {
            updateTime = val;
            return this;
        }

        public Builder auditTime(long val) {
            auditTime = val;
            return this;
        }

        public Builder auditor(String val) {
            auditor = val;
            return this;
        }

        public Builder auditorIp(String val) {
            auditorIp = val;
            return this;
        }

        public Builder auditMsg(List<String> val) {
            auditMsg = val;
            return this;
        }

        public Builder auditStatus(int val) {
            auditStatus = val;
            return this;
        }

        public Authentication build() {
            return new Authentication(this);
        }
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(this.userType);
        dest.writeByte(this.readOnly ? (byte) 1 : (byte) 0);
        dest.writeString(this.companyName);
        dest.writeString(this.legalPersonName);
        dest.writeString(this.legalPersonIdNum);
        dest.writeString(this.businessLicenseLocalUrl);
        dest.writeString(this.businessLicenseRemoteUrl);
        dest.writeString(this.legalPersonIdCardFrontLocalUrl);
        dest.writeString(this.legalPersonIdCardFrontRemoteUrl);
        dest.writeString(this.legalPersonIdCardBackLocalUrl);
        dest.writeString(this.legalPersonIdCardBackRemoteUrl);
        dest.writeString(this.personalName);
        dest.writeString(this.personalIdNum);
        dest.writeString(this.personalHoldingIdCardLocalUrl);
        dest.writeString(this.personalHoldingIdCardRemoteUrl);
        dest.writeString(this.personalIdCardFrontLocalUrl);
        dest.writeString(this.personalIdCardFrontRemoteUrl);
        dest.writeString(this.personalIdCardBackLocalUrl);
        dest.writeString(this.personalIdCardBackRemoteUrl);
        dest.writeLong(this.createTime);
        dest.writeLong(this.updateTime);
        dest.writeLong(this.auditTime);
        dest.writeString(this.auditor);
        dest.writeString(this.auditorIp);
        dest.writeStringList(this.auditMsg);
        dest.writeInt(this.auditStatus);
    }

    protected Authentication(Parcel in) {
        this.userType = in.readInt();
        this.readOnly = in.readByte() != 0;
        this.companyName = in.readString();
        this.legalPersonName = in.readString();
        this.legalPersonIdNum = in.readString();
        this.businessLicenseLocalUrl = in.readString();
        this.businessLicenseRemoteUrl = in.readString();
        this.legalPersonIdCardFrontLocalUrl = in.readString();
        this.legalPersonIdCardFrontRemoteUrl = in.readString();
        this.legalPersonIdCardBackLocalUrl = in.readString();
        this.legalPersonIdCardBackRemoteUrl = in.readString();
        this.personalName = in.readString();
        this.personalIdNum = in.readString();
        this.personalHoldingIdCardLocalUrl = in.readString();
        this.personalHoldingIdCardRemoteUrl = in.readString();
        this.personalIdCardFrontLocalUrl = in.readString();
        this.personalIdCardFrontRemoteUrl = in.readString();
        this.personalIdCardBackLocalUrl = in.readString();
        this.personalIdCardBackRemoteUrl = in.readString();
        this.createTime = in.readLong();
        this.updateTime = in.readLong();
        this.auditTime = in.readLong();
        this.auditor = in.readString();
        this.auditorIp = in.readString();
        this.auditMsg = in.createStringArrayList();
        this.auditStatus = in.readInt();
    }

    public static final Creator<Authentication> CREATOR = new Creator<Authentication>() {
        @Override
        public Authentication createFromParcel(Parcel source) {
            return new Authentication(source);
        }

        @Override
        public Authentication[] newArray(int size) {
            return new Authentication[size];
        }
    };

    public static Authentication parse(UserCertification userCertification) {
        Authentication.Builder builder = Authentication.newBuilder().readOnly(true);
        try {
            switch (userCertification.getType()) {
                case UserCertification.USER_TYPE_ORGANIZATION:
                    builder.userType(USER_TYPE_ORGANIZATION)
                            .legalPersonName(userCertification.getContent().getIDCardName())
                            .legalPersonIdNum(userCertification.getContent().getIDCardNo())
                            .companyName(userCertification.getContent().getCompanyName())
                            .businessLicenseRemoteUrl(userCertification.getContent().getBusinessLicenseUrl())
                            .legalPersonIdCardFrontRemoteUrl(userCertification.getContent().getIDCardFrontUrl())
                            .legalPersonIdCardBackRemoteUrl(userCertification.getContent().getIDCardBackUrl());
                    break;
                case UserCertification.USER_TYPE_PERSONAL:
                    builder.userType(USER_TYPE_PERSONAL)
                            .personalName(userCertification.getContent().getIDCardName())
                            .personalIdNum(userCertification.getContent().getIDCardNo())
                            .personalHoldingIdCardRemoteUrl(userCertification.getContent().getIDCardHoldUrl())
                            .personalIdCardFrontRemoteUrl(userCertification.getContent().getIDCardFrontUrl())
                            .personalIdCardBackRemoteUrl(userCertification.getContent().getIDCardBackUrl());
                    break;
                default:
                    builder.userType(USER_TYPE_UNKNOW);
                    break;
            }
        } catch (Exception e) {
            builder.userType(USER_TYPE_UNKNOW);
        }
        switch (userCertification.getAuditStatus()) {
            case UserCertification.STATUS_AUTHENTICATION_FAILED:
                builder.auditStatus(Authentication.STATUS_AUTHENTICATION_FAILED);
                break;
            case UserCertification.STATUS_AUTHENTICATION_SUCCESS:
                builder.auditStatus(Authentication.STATUS_AUTHENTICATION_SUCCESS);
                break;
            case UserCertification.STATUS_WAITING_AUTHENTICATION:
                builder.auditStatus(Authentication.STATUS_WAITING_AUTHENTICATION);
                break;
            default:
                builder.auditStatus(Authentication.STATUS_NOT_AUTHENTICATION);
                break;
        }


        return builder
                .auditMsg(ListUtil.isEmpty(userCertification.getAuditMsg()) ? new LinkedList<String>() : userCertification.getAuditMsg())
                .auditor(TextUtils.isEmpty(userCertification.getAuditor()) ? "" : userCertification.getAuditor())
                .auditorIp(TextUtils.isEmpty(userCertification.getAuditorIp()) ? "" : userCertification.getAuditorIp())
                .auditTime(userCertification.getAuditTime())
                .updateTime(userCertification.getUpdateTime())
                .createTime(userCertification.getCreateTime())
                .build();
    }
}
